package net.sxnic.item.duiying;

import java.util.List;

import net.sxinfo.core.dao.hibernate3.HibernateCriteria;
import net.sxinfo.core.dao.hibernate3.HibernateDaoUtils;
import net.sxinfo.core.spring25.Dao;
import net.sxinfo.core.spring25.DefaultManager;
import net.sxnic.item.duiying.dao.DuiyingDao;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("dyManager")
public class DefaultDuiyingManager extends
		DefaultManager<Duiying, DuiyingDao, String> implements DuiyingManager {

	@Autowired
	private DuiyingDao duiyingDao;

	@Override
	protected Dao<Duiying, String> getEntityDao() {
		return duiyingDao;
	}

	@Override
	public String findJspYear(String optYear, String catalog1Alias,
			String catalog2Code) {

		// 从小类的定义开始
		HibernateCriteria hc = new HibernateCriteria().add(HibernateDaoUtils
				.createHibernateOrder("dyYear", false));
		
		Criterion dc = Restrictions.le("cyear", optYear);
		hc.add(dc);

		dc = Restrictions.eq("cate1", catalog1Alias);
		hc.add(dc);

		dc = Restrictions.like("cate2", "%" + catalog2Code + "%");
		hc.add(dc);

		List<Duiying> list = duiyingDao.getByCriteria(hc);
		if (list != null && list.size() > 0) {
			return list.get(0).getDyYear();
		}

		// 如果小类没有定义，再找大类		
		hc = new HibernateCriteria().add(HibernateDaoUtils
				.createHibernateOrder("dyYear", false));
		
		dc = Restrictions.le("cyear", optYear);
		hc.add(dc);

		dc = Restrictions.eq("cate1", catalog1Alias);
		hc.add(dc);

		list = duiyingDao.getByCriteria(hc);
		if (list != null && list.size() > 0) {
			return list.get(0).getDyYear();
		}

		return optYear;
	}

}
